home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d13 / route.arc / PCBVIEW.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-24  |  14.5 KB  |  501 lines

  1. /*
  2. ** printed circuit board displayer, Copyright (C) Randy Nevin 1989.
  3. **
  4. ** you may give this software to anyone, make as many copies as you like, and
  5. ** post it on public computer bulletin boards and file servers. you may not
  6. ** sell it or charge any fee for distribution (except for media and postage),
  7. ** remove this comment or the copyright notice from the code, or claim that
  8. ** you wrote this code or anything derived from it. you may modify the code as
  9. ** much as you want (please document clearly with comments, and maintain the
  10. ** coding style), but programs which are derived from this one are subject to
  11. ** the conditions stated here. i am providing this code so that people can
  12. ** learn from it, so if you distribute it, please include source code, not
  13. ** just executables. contact me to report bugs or suggest enhancements; i do
  14. ** not guarantee support, but i will make an effort in good faith to help you,
  15. ** and i want to act as a central clearing house for future versions. you
  16. ** should contact me before undertaking a significant development effort, to
  17. ** avoid reinventing the wheel. if you come up with an enhancement you
  18. ** consider particularly useful, i would appreciate being informed so that it
  19. ** can be incorporated in future versions. my address is: Randy Nevin,
  20. ** 1731 211th PL NE, Redmond, WA 98053. this code is available directly from
  21. ** the author; just send a floppy and a self-addressed floppy mailer with
  22. **
  23. ** HISTORY
  24. ** (name        date        description)
  25. ** ----------------------------------------------------
  26. ** randy nevin        2/1/89        initial version
  27. ** randy nevin        2/11/89        released version 1.00
  28. */
  29.  
  30. #include <stdio.h>
  31.  
  32. #ifndef M_XENIX
  33. #include <stdlib.h>
  34. #include <io.h>
  35. #include <conio.h>
  36. #include <dos.h>
  37. #endif
  38.  
  39. #include <string.h>
  40. /* #include <signal.h> enable this to catch ^C */
  41. #include "cell.h"
  42.  
  43. /*
  44. ** ^C handling is disabled because on my system enabling it seems to interfere
  45. ** with int 10h processing, which is needed to plot pixels. you should enable
  46. ** it and see what happens on your system.
  47. */
  48.  
  49. /* WARNING: the code below assumes 640x350 16-color ega */
  50.  
  51. /* 0=black    1=blue        2=green        3=light blue        */
  52. /* 4=red    5=purple    6=brown        7=grey            */
  53. /* 8=black    9=bright blue    A=bright green    B=bright light blue    */
  54. /* C=scarlet    D=purple    E=yellow    F=white            */
  55.  
  56. /*
  57. ** the colors below work fine for me, but may not for your particular ega and
  58. ** monitor. if bright blue and light blue look the same to you, or some traces
  59. ** appear to be missing, you may want to change these constants.
  60. **
  61. ** on some egas, there appear to be gaps in the traces; i don't know why. on
  62. ** other egas, the traces look fine. this happened to me on the maximum zoom,
  63. ** but not on any other zoom level. apparently some problem with the int 10h
  64. ** interface.
  65. */
  66.  
  67. /* colors of objects */
  68. #define H    0xC    /* hole color; scarlet            */
  69. #define F    0x9    /* frontside trace color; bright blue    */
  70. #define B    0x3    /* backside trace color; light blue    */
  71. #define E    0xE    /* edge color; yellow            */
  72.  
  73. /* screen limits */
  74. #define MINHORZ    0    /* left-most pixel    */
  75. #define MAXHORZ    639    /* right-most pixel    */
  76. #define MINVERT    0    /* top-most pixel    */
  77. #define MAXVERT    349    /* bottom-most pixel    */
  78.  
  79. static int mode; /* for saving original screen mode */
  80. static int sides = 3; /* 0=holes only, 1=front only, 2=back only, 3=all */
  81.  
  82. #define MAXZOOM    3    /* maximum zoom number; minimum is 0 */
  83.  
  84. #define ZOOM0    3    /* 3x3 pixels per cell        */
  85. #define ZOOM1    6    /* 6x6 pixels per cell        */
  86. #define ZOOM2    10    /* 10x10 pixels per cell    */
  87. #define ZOOM3    18    /* 18x18 pixels per cell    */
  88.  
  89. static int zoom = 1; /* 0=3x3, 1=6x6, 2=10x10, 3=18x18 */
  90. static int zoomsize[MAXZOOM+1] = { ZOOM0, ZOOM1, ZOOM2, ZOOM3 };
  91.  
  92. /* current lower-left position */
  93. static int Xrow = 0;
  94. static int Xcol = 0;
  95.  
  96. int justboard = 1; /* only need the board data structure */
  97.  
  98. /* board dimensions */
  99. extern int Nrows;
  100. extern int Ncols;
  101.  
  102. extern void InitBoard( void );
  103. extern long GetCell( int, int, int );
  104. extern void SetCell( int, int, int, long );
  105. extern int GetMode( void );
  106. extern void SetMode( int );
  107. extern void Dot( int, int, int );
  108.  
  109. void main( int, char *[] );
  110. /* static void control_C( void ); enable this to catch ^C */
  111. static void doedges( void );
  112. static void doboard( void );
  113. static void map( int, int, long, long );
  114. static void plot0( int, int, char [ZOOM0][ZOOM0], int );
  115. static void plot1( int, int, char [ZOOM1][ZOOM1], int );
  116. static void plot2( int, int, char [ZOOM2][ZOOM2], int );
  117. static void plot3( int, int, char [ZOOM3][ZOOM3], int );
  118.  
  119. void main ( argc, argv ) /* input routed board, display it on the screen */
  120.     int argc;
  121.     char *argv[];
  122.     {
  123.     char *self, *p;
  124.     int r, c, rp, cp, i1, i2, i3, i4;
  125.     FILE *fp;
  126.     long x;
  127. #ifndef M_XENIX
  128.     union REGS regs;
  129. #else
  130.     int chh;
  131. #endif
  132.  
  133.     printf( "Copyright (C) Randy Nevin, 1989. Version 1.00\n" );
  134.     printf( "See source code for rights granted.\n\n" );
  135.     self = argv[0];
  136.     /* get rid of initial part of path */
  137.     if ((p = strrchr( self, '\\' )) || (p = strrchr( self, ':' )))
  138.         self = ++p;
  139.     if (argc != 2) { /* need infile */
  140.         fprintf( stderr, "usage: %s infile\n", self );
  141.         exit( -1 );
  142.         }
  143.     if (!(fp = fopen( argv[1], "rb" ))) {
  144.         fprintf( stderr, "can't open %s\n", argv[1] );
  145.         exit( -1 );
  146.         }
  147.     /* fetch the board dimensions */
  148.     if ((rp = getc( fp )) == EOF || (cp = getc( fp )) == EOF) {
  149.         fprintf( stderr, "premature eof\n" );
  150.         exit( -1 );
  151.         }
  152.     Nrows = (rp & 0xFF) | ((cp << 8) & 0xFF00);
  153.     if ((rp = getc( fp )) == EOF || (cp = getc( fp )) == EOF) {
  154.         fprintf( stderr, "premature eof\n" );
  155.         exit( -1 );
  156.         }
  157.     Ncols = (rp & 0xFF) | ((cp << 8) & 0xFF00);
  158.     InitBoard(); /* allocate memory for data structures */
  159.     for (r = 0; r < Nrows; r++) { /* read in the board, row by column */
  160.         for (c = 0; c < Ncols; c++) {
  161.             /* first, do frontside */
  162.             if ((i1 = getc( fp )) == EOF
  163.                 || (i2 = getc( fp )) == EOF
  164.                 || (i3 = getc( fp )) == EOF
  165.                 || (i4 = getc( fp )) == EOF) {
  166.                 fprintf( stderr, "premature eof\n" );
  167.                 exit( -1 );
  168.                 }
  169.             x = (long)i1 | (((long)i2) << 8)
  170.                 | (((long)i3) << 16) | (((long)i4) << 24);
  171.             SetCell( r, c, TOP, x );
  172.             /* then do backside */
  173.             if ((i1 = getc( fp )) == EOF
  174.                 || (i2 = getc( fp )) == EOF
  175.                 || (i3 = getc( fp )) == EOF
  176.                 || (i4 = getc( fp )) == EOF) {
  177.                 fprintf( stderr, "premature eof\n" );
  178.                 exit( -1 );
  179.                 }
  180.             x = (long)i1 | (((long)i2) << 8)
  181.                 | (((long)i3) << 16) | (((long)i4) << 24);
  182.             SetCell( r, c, BOTTOM, x );
  183.             }
  184.         }
  185.     /* tell user what commands are available */
  186.     printf( "\t0   = holes only\n" );
  187.     printf( "\t1   = holes and top traces\n" );
  188.     printf( "\t2   = holes and bottom traces\n" );
  189.     printf( "\t3   = holes and all traces\n" );
  190.     printf( "\tz/Z = zoom one level / maximum zoom\n" );
  191.     printf( "\ts/S = shrink one level / minimum shrink\n" );
  192.     printf( "\tl/L = move left by one / move left by ten\n" );
  193.     printf( "\tr/R = move right by one / move right by ten\n" );
  194.     printf( "\tu/U = move up by one / move up by ten\n" );
  195.     printf( "\td/D = move down by one / move down by ten\n" );
  196.     printf( "\tany other key exits the program\n" );
  197.     printf( "\nPress ENTER to continue, or ^C to exit " );
  198. #ifndef M_XENIX
  199.     regs.h.ah = 0x8; /* character input without echo */
  200.     intdos( ®s, ®s ); /* call dos to get a keystroke */
  201. #else
  202.     (void)getchar();
  203. #endif
  204.     mode = GetMode(); /* save mode so can restore later */
  205.     /* signal( SIGINT, control_C ); enable this to catch ^C */
  206.     SetMode( 0x10 ); /* 640x350 16-color mode */
  207.     doedges(); /* display board edges */
  208.     doboard(); /* display the board */
  209.     for (;;) { /* process until unrecognized keystroke */
  210. #ifndef M_XENIX
  211.         regs.h.ah = 0x8; /* character input without echo */
  212.         intdos( ®s, ®s ); /* call dos to get a keystroke */
  213.         switch (regs.h.al) { /* determine what it is */
  214. #else
  215.         switch (chh = getchar()) { /* determine what it is */
  216. #endif
  217.         case '0': /* just show holes */
  218.             if (sides == 0)
  219.                 continue;
  220.             sides = 0;
  221.             break;
  222.         case '1': /* show holes and top-side traces */
  223.             if (sides == 1)
  224.                 continue;
  225.             sides = 1;
  226.             break;
  227.         case '2': /* show holes and bottom-side traces */
  228.             if (sides == 2)
  229.                 continue;
  230.             sides = 2;
  231.             break;
  232.         case '3': /* show holes and all traces */
  233.             if (sides == 3)
  234.                 continue;
  235.             sides = 3;
  236.             break;
  237.         case 'Z': /* zoom to the limit */
  238.             if (zoom == MAXZOOM)
  239.                 continue;
  240.             zoom = MAXZOOM;
  241.             break;
  242.         case 'z': /* zoom by one */
  243.             if (zoom == MAXZOOM)
  244.                 continue;
  245.             zoom++;
  246.             break;
  247.         case 'S': /* shrink to the limit */
  248.             if (zoom == 0)
  249.                 continue;
  250.             zoom = 0;
  251.             break;
  252.         case 's': /* shrink by one */
  253.             if (zoom == 0)
  254.                 continue;
  255.             zoom--;
  256.             break;
  257.         case 'L': /* left by 10 */
  258.             if (Xcol == 0)
  259.                 continue;
  260.             if (Xcol <= 10)
  261.                 Xcol = 0;
  262.             else
  263.                 Xcol -= 10;
  264.             break;
  265.         case 'l': /* left by one */
  266.             if (Xcol == 0)
  267.                 continue;
  268.             Xcol--;
  269.             break;
  270.         case 'R': /* right by 10 */
  271.             if (Xcol == Ncols-1)
  272.                 continue;
  273.             if (Xcol >= Ncols-11)
  274.                 Xcol = Ncols-1;
  275.             else
  276.                 Xcol += 10;
  277.             break;
  278.         case 'r': /* right by one */
  279.             if (Xcol == Ncols-1)
  280.                 continue;
  281.             Xcol++;
  282.             break;
  283.         case 'U': /* up by 10 */
  284.             if (Xrow == Nrows-1)
  285.                 continue;
  286.             if (Xrow >= Nrows-11)
  287.                 Xrow = Nrows-1;
  288.             else
  289.                 Xrow += 10;
  290.             break;
  291.         case 'u': /* up by one */
  292.             if (Xrow == Nrows-1)
  293.                 continue;
  294.             Xrow++;
  295.             break;
  296.         case 'D': /* down by 10 */
  297.             if (Xrow == 0)
  298.                 continue;
  299.             if (Xrow <= 10)
  300.                 Xrow = 0;
  301.             else
  302.                 Xrow -= 10;
  303.             break;
  304.         case 'd': /* down by one */
  305.             if (Xrow == 0)
  306.                 continue;
  307.             Xrow--;
  308.             break;
  309.         default:
  310.             SetMode( mode ); /* restore original screen mode */
  311.             exit( 0 );
  312.             break;
  313.             }
  314.         SetMode( 0x10 ); /* clear screen */
  315.         doedges(); /* display board edges */
  316.         doboard(); /* display the board */
  317.         }
  318.     }
  319.  
  320. /*
  321. ** enable this to catch ^C
  322. **
  323. **static void control_C () { / * handle ^C * /
  324. **    SetMode( mode ); / * restore original screen mode * /
  325. **    exit( 0 );
  326. **    }
  327. */
  328.  
  329. static void doedges () { /* display the board edges */
  330.     int r1, c1, r2, c2, i, z;
  331.  
  332.     z = zoomsize[zoom];
  333.     /* first, calculate their virtual screen positions */
  334.     r1 = MAXVERT+(Xrow*z); /* bottom edge */
  335.     c1 = MINHORZ-(Xcol*z); /* left edge */
  336.     r2 = MAXVERT-1-((Nrows-Xrow)*z); /* top edge */
  337.     c2 = MINHORZ+1+((Ncols-Xcol)*z); /* right edge */
  338.     if (r1 >= MINVERT && r1 <= MAXVERT) /* draw bottom edge */
  339.         for (i = c1; i <= c2; i++)
  340.             if (i >= MINHORZ && i <= MAXHORZ)
  341.                 Dot( E, r1, i );
  342.     if (c1 >= MINHORZ && c1 <= MAXHORZ) /* draw left edge */
  343.         for (i = r1; i >= r2; i--)
  344.             if (i >= MINVERT && i <= MAXVERT)
  345.                 Dot( E, i, c1 );
  346.     if (r2 >= MINVERT && r2 <= MAXVERT) /* draw top edge */
  347.         for (i = c1; i <= c2; i++)
  348.             if (i >= MINHORZ && i <= MAXHORZ)
  349.                 Dot( E, r2, i );
  350.     if (c2 >= MINHORZ && c2 <= MAXHORZ) /* draw right edge */
  351.         for (i = r1; i >= r2; i--)
  352.             if (i >= MINVERT && i <= MAXVERT)
  353.                 Dot( E, i, c2 );
  354.     }
  355.  
  356. static void doboard () { /* display the board on the screen, row by column */
  357.     int r, c, rp, cp, rpd, cpd, z;
  358.     long x, y;
  359.  
  360.     z = zoomsize[zoom];
  361.     rpd = MINVERT+z; /* top-most plottable row */
  362.     cpd = MAXHORZ-z; /* right-most plottable column */
  363.     for (r = Xrow, rp = MAXVERT-1; r < Nrows && rp >= rpd; r++, rp -= z) {
  364.         for (c = Xcol, cp = MINHORZ+1; c < Ncols && cp <= cpd;
  365.                 c++, cp += z) {
  366.             x = GetCell( r, c, TOP );
  367.             y = GetCell( r, c, BOTTOM );
  368.             if (x || y) /* only map if something is there */
  369.                 map( rp, cp, x, y );
  370.             }
  371.         }
  372.     }
  373.  
  374. struct x { /* group the bit templates for an object */
  375.     long t;            /* the object type    */
  376.     char t0[ZOOM0][ZOOM0];    /* tiny zoom template    */
  377.     char t1[ZOOM1][ZOOM1];    /* small zoom template    */
  378.     char t2[ZOOM2][ZOOM2];    /* medium zoom template    */
  379.     char t3[ZOOM3][ZOOM3];    /* large zoom template    */
  380.     };
  381.  
  382. extern struct x y1[];  /* hole templates        */
  383. extern struct x y2[];  /* hole-related templates    */
  384. extern struct x y3[];  /* non-hole-related templates    */
  385.  
  386. extern int z1;  /* number of hole types            */
  387. extern int z2;  /* number of hole-related types        */
  388. extern int z3;  /* number of non-hole-related types    */
  389.  
  390. #define domap1(v,c)    { for (i = 0; i < z1; i++) { \
  391.                 if (v & (y1[i].t)) { \
  392.                     if (zoom == 0) \
  393.                         plot0( rp, cp, y1[i].t0, c );\
  394.                     else if (zoom == 1) \
  395.                         plot1( rp, cp, y1[i].t1, c );\
  396.                     else if (zoom == 2) \
  397.                         plot2( rp, cp, y1[i].t2, c );\
  398.                     else if (zoom == 3) \
  399.                         plot3( rp, cp, y1[i].t3, c );\
  400.                     } \
  401.                 } } \
  402.  
  403. #define domap2(v,c)    { for (i = 0; i < z2; i++) { \
  404.                 if (v & (y2[i].t)) { \
  405.                     if (zoom == 0) \
  406.                         plot0( rp, cp, y2[i].t0, c );\
  407.                     else if (zoom == 1) \
  408.                         plot1( rp, cp, y2[i].t1, c );\
  409.                     else if (zoom == 2) \
  410.                         plot2( rp, cp, y2[i].t2, c );\
  411.                     else if (zoom == 3) \
  412.                         plot3( rp, cp, y2[i].t3, c );\
  413.                     } \
  414.                 } } \
  415.  
  416. #define domap3(v,c)    { for (i = 0; i < z3; i++) { \
  417.                 if (v & (y3[i].t)) { \
  418.                     if (zoom == 0) \
  419.                         plot0( rp, cp, y3[i].t0, c );\
  420.                     else if (zoom == 1) \
  421.                         plot1( rp, cp, y3[i].t1, c );\
  422.                     else if (zoom == 2) \
  423.                         plot2( rp, cp, y3[i].t2, c );\
  424.                     else if (zoom == 3) \
  425.                         plot3( rp, cp, y3[i].t3, c );\
  426.                     } \
  427.                 } } \
  428.  
  429. static void map ( rp, cp, v0, v1 ) /* map a cell */
  430.     int rp, cp;
  431.     long v0, v1;
  432.     {
  433.     int i;
  434.  
  435.     if (v0 & HOLE) {
  436.         domap1( v0, H ); /* plot the hole */
  437.         if (v1 && (sides & 2)) /* plot backside? */
  438.             domap2( v1, B );
  439.         if (v0 && (sides & 1)) /* plot frontside? */
  440.             domap2( v0, F );
  441.         }
  442.     else {
  443.         if (v1 && (sides & 2)) /* plot backside? */
  444.             domap3( v1, B );
  445.         if (v0 && (sides & 1)) /* plot frontside? */
  446.             domap3( v0, F );
  447.         }
  448.     }
  449.  
  450. static void plot0 ( rp, cp, obj, color ) /* plot a 3x3 template */
  451.     int rp, cp;
  452.     char obj[ZOOM0][ZOOM0];
  453.     int color;
  454.     {
  455.     int r, c;
  456.  
  457.     for (r = 0; r < ZOOM0; r++)
  458.         for (c = 0; c < ZOOM0; c++)
  459.             if (obj[r][c])
  460.                 Dot( color, rp-r, cp+c );
  461.     }
  462.  
  463. static void plot1 ( rp, cp, obj, color ) /* plot a 6x6 template */
  464.     int rp, cp;
  465.     char obj[ZOOM1][ZOOM1];
  466.     int color;
  467.     {
  468.     int r, c;
  469.  
  470.     for (r = 0; r < ZOOM1; r++)
  471.         for (c = 0; c < ZOOM1; c++)
  472.             if (obj[r][c])
  473.                 Dot( color, rp-r, cp+c );
  474.     }
  475.  
  476. static void plot2 ( rp, cp, obj, color ) /* plot a 10x10 template */
  477.     int rp, cp;
  478.     char obj[ZOOM2][ZOOM2];
  479.     int color;
  480.     {
  481.     int r, c;
  482.  
  483.     for (r = 0; r < ZOOM2; r++)
  484.         for (c = 0; c < ZOOM2; c++)
  485.             if (obj[r][c])
  486.                 Dot( color, rp-r, cp+c );
  487.     }
  488.  
  489. static void plot3 ( rp, cp, obj, color ) /* plot an 18x18 template */
  490.     int rp, cp;
  491.     char obj[ZOOM3][ZOOM3];
  492.     int color;
  493.     {
  494.     int r, c;
  495.  
  496.     for (r = 0; r < ZOOM3; r++)
  497.         for (c = 0; c < ZOOM3; c++)
  498.             if (obj[r][c])
  499.                 Dot( color, rp-r, cp+c );
  500.     }
  501.